var database = require('../../data/data.js');
Page({
  data: {
    show: false,
    loading: "none"
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    var id = options.id;
    var types = options.type;
    if (options.date != null) {
      this.setData({
        date: options.date
      })
    } else {
      var date1 = new Date();
      var month = "";
      if (date1.getMonth() < 9) {
        month = "0" + (parseInt(date1.getMonth()) + 1)
      } else {
        month = parseInt(date1.getMonth()) + 1
      }
      this.setData({
        date: date1.getFullYear()+month,
      })
    }
    wx.login({
      success(res) {
        var code = res.code;
        wx.request({
          url: database.url + "/api/V2/user/access_token",
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            var pageNum = that.data.pageNum
            if (database.unionid == "") {
              wx.request({
                url: database.url + '/api/V2/integral/integral_openid',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  accessToken: database.accessToken,
                  weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
                  regionName: "other"
                },
                success(res) {
                  var sessoionKey = res.data.data.session_key;
                  wx.getUserInfo({
                    data: {
                      withCredentials: true,
                      lang: "zh_CN"
                    },
                    success(res) {
                      wx.request({
                        url: database.url + '/api/V2/user/we_chat_user',
                        method: "POST",
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                          accessToken: database.accessToken,
                          weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
                          encryptedData: res.encryptedData,
                          iv: res.iv,
                          sessionKey: sessoionKey,
                          weChatName: "足球投票"
                        },
                        success(res) {
                          database.unionid = res.data.data.unionId;
                          wx.request({
                            url: database.url + '/api/V2/integral/get_integral',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              regionName: "other",
                              id: id,
                              unionid: database.unionid,
                              yearMonth:that.data.date
                            },
                            success(res) {
                              wx.hideLoading();
                              var teamDetail = res.data.data;
                              that.setData({
                                loading: "",
                                type: types,
                                id: id,
                                teamDetail: teamDetail
                              })
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            } else {
              wx.request({
                url: database.url + '/api/V2/integral/get_integral',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  accessToken: database.accessToken,
                  regionName: "other",
                  id: id,
                  unionid: database.unionid,
                  yearMonth: that.data.date
                },
                success(res) {
                  wx.hideLoading();
                  var teamDetail = res.data.data;
                  that.setData({
                    type: types,
                    id: id,
                    teamDetail: teamDetail,
                    loading: ""
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  toEdit: function (e) {
    wx.navigateTo({
      url: '/pages/my/myteam/edit/edit?id=' + e.currentTarget.dataset.id,
    })
  },
  toHome: function () {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  showAll: function (e) {
    if (this.data.show) {
      this.setData({
        show: false
      })
    } else {
      this.setData({
        show: true
      })
    }

  },
  vote: function (e) {
    var that = this;
    var vote = {
      id: e.currentTarget.dataset.id
    }
    database.vote[database.vote.length] = vote;
    wx.showLoading({
      title: '正在保存',
    })
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: database.url + '/api/V2/integral/integral_vote',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        softballId: id,
        weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
        unionid: database.unionid,
        integralGenre: "TouPiao"
      },
      success(res) {
        if (res.data.status_code == 200) {
          var teamDetail = that.data.teamDetail;
          teamDetail.integralTotal = parseInt(teamDetail.integralTotal) + 1;
          that.setData({
            teamDetail: teamDetail
          })
        } else {

        }
        wx.hideLoading();
        wx.showToast({
          icon: "none",
          title: res.data.status_reason,
        })
      }
    })
  },
  onShareAppMessage: function (e) {
    var that = this;
    wx.request({
      url: database.url + '/api/V2/integral/integral_vote',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        softballId: that.data.teamDetail.id,
        weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
        unionid: database.unionid,
        integralGenre: "FenXiang"
      },
      success(res) {
      }
    })
    return {
      title: that.data.teamDetail.title + '正在参加全国球队投票排名，请帮我们投上一票',
      imageUrl: '/img/shareTeam.png',
      path: '/pages/team_detail/team_detail?id=' + that.data.teamDetail.id + '&type=share',
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})